package com.sxdx.convert;

import org.springframework.beans.BeanUtils;

public class DtoToEntityConverter {

    /**
     * 将DTO对象转换为实体类对象
     *
     * @param dto   DTO对象
     * @param clazz 实体类的Class对象
     * @param <T>   实体类类型
     * @return 转换后的实体类对象
     */
    public static <T> T convertDtoToEntity(Object dto, Class<T> clazz) {
        if (dto == null) {
            return null;
        }

        try {
            T entity = clazz.newInstance();
            BeanUtils.copyProperties(dto, entity);
            return entity;
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException("Failed to convert DTO to entity.", e);
        }
    }
}
